<template>
    <div>
        <el-dialog title="新增" v-model="isShowDialog" width="600px" @close="closeDialog">
            <el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
                <el-form-item label="工作空间" prop="workspaceId">
                    <el-input v-model="formModel.workspaceId" show-word-limit maxlength="100" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="节点名称" prop="nodeName">
                    <el-input v-model="formModel.nodeName" show-word-limit maxlength="32" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="IP" prop="ip">
                    <el-input v-model="formModel.ip" show-word-limit maxlength="32" placeholder="" clearable></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="() => closeDialog()" size="default">取 消</el-button>
                    <el-button type="primary" :loading="submitLoading" @click="onSubmit" size="default">提 交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { RegisterInfoAddParamModel } from '/@/interfaces/JdfsystemModel';
import { handleAdd, handleValidateForm } from '../register-info-wrap';

interface State {
    isShowDialog: boolean | true,
    submitLoading: boolean | false,
    formModel: RegisterInfoAddParamModel,
    formRules: any,
}

export default defineComponent({
    name: 'RegisterInfoAdd',
    components: {},
    setup(props, context) {
        const state = reactive<State>({
            isShowDialog: true,
            submitLoading: false,
            formModel: {
                workspaceId: undefined,
                nodeName: undefined,
                ip: undefined,
            },
            formRules: {
                workspaceId: [{ required: true, message: '工作空间不能为空', trigger: 'change' }],
                nodeName: [{ required: true, message: '节点名称不能为空', trigger: 'change' }],
                ip: [{ required: true, message: 'IP不能为空', trigger: 'change' }]
            },
        });
        const methodMap = reactive({
            // 关闭弹窗
            closeDialog: function () {
                state.isShowDialog = false;
                context.emit('onClose');
            },
            // 新增
            onSubmit: async function () {
                setTimeout(() => state.submitLoading = false, 1500);
                // 表单验证
                let validate = await handleValidateForm(formRef.value);
                if (!validate) {
                    state.submitLoading = false;
                    return;
                }
                // 提交
                await handleAdd({ param: state.formModel, pageInfo: undefined }, methodMap.closeDialog);
            },
        });
        const formRef = ref<FormInstance>();
        const validFlagRef = ref();
        return {
            validFlagRef,
            formRef,
            ...toRefs(state),
            ...toRefs(methodMap),
        };
    },
});
</script>
